<template>
    <div v-show="modelValue">
        <el-image-viewer
            v-if="previewLists.length"
            :url-list="previewLists"
            hide-on-click-modal
            teleported
            @close="handleClose"
        > 
        </el-image-viewer>
    </div>
</template>

<script setup>
  import { shallowRef,computed,watch,ref,nextTick } from 'vue';
const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false
    },
    url: {
        type: String,
        default: ''
    },
    type: {
        type: String,
        default: 'image'
    }
})
 
const emit = defineEmits(["update:modelValue"])
const playerRef = shallowRef()

const visible = computed({
    get() {
        return props.modelValue
    },

    set(value) {
        emit('update:modelValue', value)
    }
})

const handleClose = () => {
    emit('update:modelValue', false)
}

const previewLists = ref([])

watch(
    () => props.modelValue,
    (value) => {
        if (value) {
            nextTick(() => {
                previewLists.value = [props.url]
                playerRef.value?.play()
            })
        } else {
            nextTick(() => {
                previewLists.value = []
                playerRef.value?.pause()
            })
        }
    }
)
</script>


 